package com.seveneight.baijiesteward.ui.main.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.adapter.SelectBankAdapter;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.bean.AnyBean;
import com.seveneight.baijiesteward.bean.BankListBean;
import com.seveneight.baijiesteward.http.MyCallBack;
import com.seveneight.baijiesteward.ui.user.model.SeleBankListModel;
import com.seveneight.baijiesteward.ui.user.model.SeleThmemListModel;
import com.seveneight.baijiesteward.utils.ToastUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 选择银行
 */
public class SelectBankActivity extends BaseActivity implements View.OnClickListener,AdapterView.OnItemClickListener{

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.select_bank_list)
    ListView listView;
    List<BankListBean> listData=new ArrayList<>();
    List<BankListBean> seleData=new ArrayList<>();
    SelectBankAdapter mAdapter;
    String title_name;
    SeleBankListModel seleBankListModel;
    SeleThmemListModel seleThmemListModel;

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_select_bank);
        title_name=getIntent().getStringExtra("title_name");
    }

    @Override
    protected void initView() {
        mAdapter=new SelectBankAdapter(this,listData);
        listView.setAdapter(mAdapter);
//        listView.setOnItemClickListener(this);

    }

    @Override
    protected void initData() {
        tv_title.setText(title_name);
        if (title_name.contains("银行")){
            seleBankListModel=new SeleBankListModel();
            seleBankListModel.getBankList(this, jsonFormat.commonParam(), new MyCallBack<AnyBean>() {
                @Override
                public void onSuccess(AnyBean anyBean) {
                    listData= JSON.parseArray(anyBean.getBankList(),BankListBean.class);
                    mAdapter.refresh(listData);
                }

                @Override
                public void onFailed() {

                }
            });
        }else {
            seleThmemListModel=new SeleThmemListModel();
            seleThmemListModel.getBankList(this, jsonFormat.commonParam(), new MyCallBack<AnyBean>() {
                @Override
                public void onSuccess(AnyBean anyBean) {
                    listData= JSON.parseArray(anyBean.getBankTopicList(),BankListBean.class);
                    mAdapter.refresh(listData);
                }

                @Override
                public void onFailed() {

                }
            });
        }

    }

    @Override
    protected void setListener() {

    }

    /**
     * 选择银行
     * @param bean
     */
    public void setSeleData(BankListBean bean,int index){
        if (bean.getSelect()){
            for (int i=0;i<seleData.size();i++){
                if (seleData.get(i).getId().equals(bean.getId())){
                    seleData.remove(i);
                    break;
                }
            }
        }else {
            if (seleData.size()>=3){
                ToastUtil.show(this,"最多选择三项");
                return;
            }
            seleData.add(bean);
        }

        bean.setSelect(!bean.getSelect());
        listData.set(index,bean);
        mAdapter.refresh(listData);
    }

    @OnClick({R.id.bt_submit,R.id.iv_back})
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_submit:
                if (seleData.size()==0){
                    ToastUtil.show(this,"请至少选择一项");
                    return;
                }
                Intent intent=new Intent();
                intent.putExtra("selData", (Serializable) seleData);
                setResult(RESULT_OK,intent);
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        listData.get(i).setSelect(!listData.get(i).getSelect());
        setSeleData(listData.get(i),i);
        mAdapter.refresh(listData);
    }
}
